import { CustomerService } from 'src/app/@core/services/customer/customer.service';
import { Customer } from 'src/app/@core/data/customer';
import { Component, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { EditSettlementMethedComponent } from '../../basic-data/settlement-method/edit-settlement-methed/edit-settlement-methed.component';
import { AddSettlementPeriodComponent } from './add-settlement-period/add-settlement-period.component';
import { EditPeriodComponent } from './edit-period/edit-period.component';

@Component({
  selector: 'app-settlement-period',
  templateUrl: './settlement-period.component.html',
  styleUrls: ['./settlement-period.component.scss']
})
export class SettlementPeriodComponent {
  tableData:any[]=[]
  selectoptions = [
    '收款','付款'
  ]
  selectvalue = ''
  allCheckedStatus = false;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  checkedIds = [];
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any; }[];
  config = {
    id: 'Add',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  TreeData:any = [];
  filterData={
    PageSize:20,
    PageNumber:1,
    Name:'',
    SearchStatus:'',
  }
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  constructor(private dialogService: DialogService,private SharedMethodService:SharedMethodService,private CustomerService:CustomerService){}
  ngOnInit(): void {
    this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
      this.tableData = data.items
      this.pager.total = data.total
    })
  }
  dataTableOptions = {
    columns: [
      {
        field: 'code',
        header: '操作',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '收付账期',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '类型',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '账期',
        fieldType: 'text'
      },
      {
        field: 'name',
        header: '备注',
        fieldType: 'text'
      },
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
  ];//表格宽度配置
  pageIndexChange($event: any) {

  }//切换页码
  pageSizeChange($event: any){

  }//改变每页显示数量
  Delete(rowitem:any,dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      title: '删除结算周期',
      content: '确认删除该结算周期？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.CustomerService.DeleteCollectionPeriod(rowitem.collectionPeriodId).subscribe((data:any)=>{
              this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
                this.tableData = data.items
                this.pager.total = data.total
              })
              results.modalInstance.hide();
            })
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//删除
  enable(){

  }//启用
  disable(){

  }//禁用
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  Creat(dialogtype?:string){
    let data = {
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data:data,
      title: '添加',
      content: AddSettlementPeriodComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.form.collectionPeriodName&&results.modalContentInstance.form.collectionPeriodClass!==null&&results.modalContentInstance.form.collectionPeriodDuration&& results.modalContentInstance.form.receiptOrPayment!==null){
              this.CustomerService.CreateCollectionPeriod(results.modalContentInstance.form).subscribe((data:any)=>{
                this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
                  this.tableData = data.items
                  this.pager.total = data.total
                })
                results.modalInstance.hide();
              })
            }else{
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  Edit(rowItem:any,dialogtype?:string){
    let data = {
      data:rowItem,
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data:data,
      title: '更新结算周期',
      content: EditPeriodComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.form.collectionPeriodName&&results.modalContentInstance.form.collectionPeriodClass!==null&&results.modalContentInstance.form.collectionPeriodDuration&& results.modalContentInstance.form.receiptOrPayment!==null){
              this.CustomerService.UpdateCollectionPeriod(rowItem.collectionPeriodId,results.modalContentInstance.form).subscribe((data:any)=>{
                this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
                  this.tableData = data.items
                  this.pager.total = data.total
                })
                results.modalInstance.hide();
              })
            }else{
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//更新
  filter(){
    this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
      this.tableData = data.items
      this.pager.total = data.total
    })
  }//搜索
  reset(){
    this.filterData.Name = ''
    this.filterData.SearchStatus = ''
    this.CustomerService.GetPagedCollectionPeriod(this.filterData).subscribe((data:any)=>{
      this.tableData = data.items
      this.pager.total = data.total
    })
  }//重置
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable,'id').subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }//获取选中行
  cleared(){
    this.SharedMethodService.cleared(this.checkedIds,this.tableData,this.datatable).subscribe((data:any)=>{
      this.checkedIds=data;

    })
  }//清空选中行
}
